package server;

import com.sun.net.httpserver.HttpExchange;
import java.util.Map;

public class ClientProcessor
{

    Player player = null;

    public ClientProcessor(Player player)
    {
        //System.out.println("Creating processor");
        this.player = player;
    }
    

    public String processClient(String request)
    {
        String response = handleRequest(request);
        //System.out.println("response =" + response);
        return response;
    }

    private String handleRequest(String request)
    {
        String ret = "";
        String reqArr[] = request.split("\\^");
        String command = reqArr[0];
        //System.out.println("command = "+command);
        if (command.equals("login"))
        {
            ret = processLoginRequest(request);
        }
        if (command.equals("invite"))
        {
            ret = processInviteRequest(request);
        }
        if (command.equals("getUserList"))
        {
            ret = processGetUserListRequest(request);
        }
        if (command.equals("getInvite"))
        {
            ret = processGetInviteRequest(request);
        }
        if (command.equals("answerInvite"))
        {
            ret = processAnswerInviteRequest(request);
        }
        if (command.equals("checkUpdate"))
        {
            System.out.println("In checkUpdate if"); 
            ret = processCheckUpdateRequest(request);
        }
        if (command.equals("move"))
        {
            ret = processMoveRequest(request);
        }

        return ret;

    }
    
    private String processLoginRequest(String request)
    {
        //C: login^ClientName\n
        //S: login^NO\n
        //S:  login^OK^nick:0;nick1:1;nick2:0;…\n

        String ret = "";
        String clientName = request.split("\\^")[1];
        ret = "login^OK^" + getPlayerList();
        return ret;
    }

    private String processInviteRequest(String request)
    {
        //invite^ClientName1^ClientName2\n
        //invite^ClientName2^YES^X_or_O\n
        //invite^ClientName2^NO
        String ret = "";
        String clientName1 = request.split("\\^")[1];
        String clientName2 = request.split("\\^")[2];
        Player player2 = null;
        this.player.status = "inviting";
        for (Map.Entry<Object, Object> entry : Server.PlayerList.entrySet())
        {
            Player tmpPlayer = (Player) entry.getKey();
            if (clientName2.equals(tmpPlayer.name))
            {
                tmpPlayer.status = "invited^" + clientName1;
                player2 = tmpPlayer;
            }
        }
        ret = "invite^" + clientName2 + "^NO";
        if (player2 != null)
        {
            int count = 0;
            while (count < 20)
            {
                if ("agree".equals(player2.status))
                {
                    TicTacToeRoom gameroom = new TicTacToeRoom(this.player, player2);
                    gameroom.gameStatus="on";
                    this.player.gameRoom = gameroom;
                    player2.gameRoom = gameroom;
                    player2.status = "engaged";
                    this.player.status = "engaged";
                    ret = "invite^" + clientName2 + "^YES^X";
                    break;
                } else if ("free".equals(player2.status))
                {
                    this.player.status = "free";
                    break;
                }
                try
                {
                    Thread.sleep(1000);
                } catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
                count++;
            }
        }

        return ret;
    }

    private String processGetUserListRequest(String request)
    {
        //getUserList^ClientName\n
        //UserList^nick1:0;nick2:1;…\n
        String ret = "UserList^" + this.getPlayerList();
        return ret;
    }

    private String processGetInviteRequest(String request)
    {
        //getInvite^ClientName\n
        //invite^YES^[Nickname of inviting player]\n
        //invite^NO\n
        String ret = "invite^NO";
        String inviteStatus = this.player.status.split("\\^")[0];
        if (inviteStatus.equals("invited"))
        {
            String playerName = this.player.status.split("\\^")[1];
            ret = "invite^YES^" + playerName;
        }
        return ret;
    }

    private String processAnswerInviteRequest(String request)
    {
        //C: answerInvite^ClientName1^ClientName2^YES\n
        //S: answerInvite^OK^X_or_O
        //            OR
        //C: answerInvite^ClientName1^ClientName2^NO\n
        //S: answerInvite^OK
        String ret = "";
        String answer = request.split("\\^")[3];
        if (answer.equals("NO"))
        {
            this.player.status = "free";
            ret = "answerInvite^OK";
        }
        if (answer.equals("YES"))
        {
            this.player.status = "agree";
            ret = "answerInvite^OK^X";
        }
        return ret;
    }

    private String processCheckUpdateRequest(String request)
    {
        //C: ckeckUpdate^ClientName\n
        //S: update^game^int;int;int;int;int;int;int;int;int;^[Nickname of moving player]\n
        //           OR
        //C: ckeckUpdate^ClientName\n
        //S: update^endGame^int;int;int;int;int;int;int;int;int;^[Nickname of Winner]\n
        //           OR
        //C: ckeckUpdate^ClientName\n
        //S: update^standoff^int;int;int;int;int;int;int;int;int;\n

        //System.out.println("In processCheckUpdateRequest().");
        String ret = "update^";
        if (this.player.gameRoom.gameStatus.equals("on"))
        {
            //System.out.println("In on brunch");
            ret += "game^" + this.player.gameRoom.getMatrix() + "^" + this.player.gameRoom.activePlayer;
        }        
        if (this.player.gameRoom.gameStatus.equals("finished"))
        {
            ret += "endGame^" + this.player.gameRoom.getMatrix() + "^" + this.player.gameRoom.winnerName;
        }
        if (this.player.gameRoom.gameStatus.equals("standoff"))
        {
            ret += "standoff^" + this.player.gameRoom.getMatrix();
        }
        return ret;
    }

    private String processMoveRequest(String request)
    {
        //move^ClientName^int\n здесь int = 0-8
        //move^game^int;int;int;int;int;int;int;int;int;^[Nickname of moving player]\n
        //             OR
        //move^ClientName^int\n здесь int = 0-8
        //move^endGame^int;int;int;int;int;int;int;int;int;^[Nickname of Winner]\n
        //           OR
        //C: move^ClientName\n
        //S: move^standoff^int;int;int;int;int;int;int;int;int;\n
        
        //System.out.println("In processMoveRequest().");
        
        int n = Integer.parseInt(request.split("\\^")[2]);        
        this.player.gameRoom.doMove(this.player.name, n);
        String ret = "move^";
        if (this.player.gameRoom.gameStatus.equals("on"))
        {
            //System.out.println("In on brunch");
            ret += "game^" + this.player.gameRoom.getMatrix() + "^" + this.player.gameRoom.activePlayer;
        }
        if (this.player.gameRoom.gameStatus.equals("finished"))
        {
            ret += "endGame^" + this.player.gameRoom.getMatrix() + "^" + this.player.gameRoom.winnerName;
        }
        if (this.player.gameRoom.gameStatus.equals("standoff"))
        {
            ret += "standoff^" + this.player.gameRoom.getMatrix();
        }
        return ret;
    }

    private String getPlayerList()
    {
        //nick:0;nick1:1;nick2:0;…\n
        StringBuilder list = new StringBuilder();
        for (Map.Entry<Object, Object> entry : Server.PlayerList.entrySet())
        {
            Player tmpPlayer = (Player) entry.getKey();
            list.append(tmpPlayer.name);
            if (tmpPlayer.status.equals("free"))
            {
                list.append(":0;");
            } else
            {
                list.append(":1;");
            }
        }
        return list.toString();
    }
}
